package com.markus.code.exam.qianxin;

import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/12 10:36
 * todo:
 */
public class Main {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * M包糖果，抛M次硬币，硬币连续n次为正面，最多能得到多少颗糖果
     * @param candies int整型一维数组 每包糖果的数量
     * @param coin int整型一维数组 抛硬币的结果
     * @param n int整型 连续是正面的次数
     * @return int整型
     */
    public int maxCandies (int[] candies, int[] coin, int n) {
        // write code here
        if (candies == null || candies.length == 0 || coin == null || coin.length == 0){
            return 0;
        }
        return process(candies,coin,0,n,0);
    }
    private int process(int [] candies,int []coin,int index,int n,int sum){
        if (index >= coin.length){
            return sum;
        }
        if (coin[index] == 0){
            sum = process(candies,coin,index+1,n,sum+candies[index]);
        }else{
            int sumNo = process(candies,coin,index+1,n,sum);
            int sumYes = 0;
            if (n != 0){
                for (int i=0;i<n;i++){
                    if (index+i < coin.length){
                        sum += candies[index+i];
                    }else{
                        break;
                    }
                }
                sumYes = process(candies,coin,index+n,0,sum);
            }
            sum = Math.max(sumYes,sumNo);
        }
        return sum;
    }

    public static void main(String[] args) {
        int []candies = {3,5,7,2,8,8,15,3};
        int []coin = {1,0,1,0,1,0,1,0};
        int n = 3;
        Main demo = new Main();
        System.out.println(demo.maxCandies(candies,coin,n));
    }
}
